package timing.ukulele.auth.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import timing.ukulele.auth.persistent.OAuth2ClientScope;
import timing.ukulele.auth.service.OAuth2ClientScopeService;
import timing.ukulele.auth.vo.OAuth2ClientScopeVO;
import timing.ukulele.common.data.ResponseData;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/scope")
@Tag(name = "OAuth2客户端的scope", description = "只能由开发人员操作")
@ApiResponses(value = {
        @ApiResponse(responseCode = "1000", description = "成功", content = {@Content(mediaType = "application/json")}),
        @ApiResponse(responseCode = "1001", description = "失败", content = {@Content(mediaType = "application/json")}),
        @ApiResponse(responseCode = "3000", description = "业务处理失败", content = {@Content(mediaType = "application/json")})
})
public class OAuth2ClientScopeController {
    private final OAuth2ClientScopeService service;

    public OAuth2ClientScopeController(OAuth2ClientScopeService service) {
        this.service = service;
    }

    @Operation(summary = "根据id查询scope信息", description = "根据id查询scope信息")
    @GetMapping("/{id}")
    public ResponseData<OAuth2ClientScopeVO> getById(
            @Parameter(description = "id", required = true, in = ParameterIn.PATH, example = "1") @PathVariable("id") Long id) {
        OAuth2ClientScope po = this.service.getById(id);
        if (po != null) {
            OAuth2ClientScopeVO vo = new OAuth2ClientScopeVO();
            BeanUtils.copyProperties(po, vo);
            return ResponseData.success(vo);
        }
        return ResponseData.error("不存在该scope");
    }

    @Operation(summary = "获取搜友的scope列表", description = "获取搜友的scope列表")
    @GetMapping()
    public ResponseData<List<OAuth2ClientScopeVO>> getAll() {
        List<OAuth2ClientScope> poList = this.service.lambdaQuery().list();
        if (!CollectionUtils.isEmpty(poList)) {
            List<OAuth2ClientScopeVO> voList = new ArrayList<>(poList.size());
            poList.forEach(po -> {
                OAuth2ClientScopeVO vo = new OAuth2ClientScopeVO();
                BeanUtils.copyProperties(po, vo);
                voList.add(vo);
            });
            return ResponseData.success(voList);
        }
        return ResponseData.success();
    }

    @Parameters(value = {
            @Parameter(name = "account", description = "当前用户", in = ParameterIn.HEADER),
    })
    @Operation(summary = "新增scope", description = "name必须是唯一的")
    @PostMapping()
    public ResponseData<String> add(@RequestBody OAuth2ClientScopeVO vo, @RequestHeader("X-USER") String account) {
        if (vo == null || vo.getId() != null || !StringUtils.hasText(vo.getName()) || !StringUtils.hasText(vo.getDescription())) {
            return ResponseData.paramError();
        }
        OAuth2ClientScope po = new OAuth2ClientScope();
        po.setName(vo.getName());
        po.setDescription(vo.getDescription());
        po.setCreateBy(account);
        po.setUpdateBy(account);
        this.service.save(po);
        return ResponseData.success();
    }

    @Operation(summary = "修改scope", description = "只能修改描述")
    @PutMapping()
    public ResponseData<String> modify(@RequestBody OAuth2ClientScopeVO vo, @RequestHeader("X-USER") String account) {
        if (vo == null || vo.getId() == null) {
            return ResponseData.paramError();
        }
        if (!StringUtils.hasText(vo.getDescription()) || StringUtils.hasText(vo.getName())) {
            return ResponseData.paramError("只能修改描述");
        }
        OAuth2ClientScope po = new OAuth2ClientScope();
        po.setId(vo.getId());
        po.setDescription(vo.getDescription());
        po.setUpdateBy(account);
        this.service.updateById(po);
        return ResponseData.success();
    }

    @Operation(summary = "删除scope", description = "物理删除")
    @DeleteMapping("/{id}")
    public ResponseData<Boolean> delete(@Parameter(description = "id", required = true, in = ParameterIn.PATH, example = "1") @PathVariable("id") Long id) {
        this.service.removeById(id);
        return ResponseData.success(Boolean.TRUE);
    }
}
